<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/15
 * Time: 12:59
 * 接口控制器，flower项目的所有接口都写在这个控制器中
 */

namespace api\flower\controller;
define('url','http://pay.yzztnet.com/flowers');
use think\Controller;
use think\Db;
use api\flower\model;
use think\Session;
//require_once 'wxpay/example/jsapi.php';

class InterfaceController extends Controller
{

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        header("Access-Control-Allow-Origin: *");//允许跨域
        header("Content-type: text/html; charset=utf-8");
    }

    /**
     * 获取花的种类接口
     */
    public function getFlowerType(){
        $flowertype = new model\FlowerFlowertypeModel();
        $result = $flowertype->field('id,name,goodtype')->where('status',0)->select();
        $count = $flowertype->count();
        if($count>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示没有查到数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=array();
        }
        json($data)->send();
    }

    /**
     * 获取首页的数据
     */
    public function getHomepage(){
        $flowertype = new model\FlowerFlowertypeModel();
        $flowerinfo = new model\FlowerFlowerinfoModel();
        $result = $flowertype->field('id,name,goodtype')->where('status',0)->select();
        $result = json_encode($result);//object先转成json,再由json转成数组
        $result = json_decode($result);
        /*  $data = $result[0];
          $data = $data->id;*/
        $arr=[];
        foreach($result as $k =>$v){
            $res = $v;
            if($res->goodtype=='1'){
                $strvalue  = $flowerinfo->where('typeid',$res->id)->field('id,name,title,goodinfo,coverimg,imgs,price,price_first,details')->limit(4)->select();
                $strvalue2 = $flowertype->where('id',$res->id)->field('id as typeid,name,homepage,description,homepagetitile,bottomprice')->select();
                $arr[$res->id] = array('title'=>$strvalue2,'content'=>$strvalue);
            }else{
                $arr[$res->id] = $flowertype->where('id',$res->id)->field('id as typeid,name,homepage,description,homepagetitile,bottomprice')->select();
            }
        }
        $data['code']='1001';
        $data['status']='1';
        $data['data']=$arr;
        json($data)->send();
    }

    /**
     *
     */
    public function getlunbo(){
        $res = Db::name('flower_homepage')->find();
        if(!empty($res['images'])){
            $imgs = stripslashes($res['images']);//addslashes(json_encode($data['imgs']));
            $imgs = json_decode($imgs);
            foreach($imgs as $k=>$v){
                $imgs[$k] = cmf_get_image_url($v);
            }
            $data['code']='1001';
            $data['status']='1';
            $data['data']=$imgs;
            json($data)->send();
        }
    }
    /**
     * 根据花卉的种类获取花卉
     */
    public function getFlowersByType(){
        $id = input('typeid');
        if(isset($id)){
            $flowertype = new model\FlowerFlowertypeModel();
            $flower = new model\FlowerFlowerinfoModel();
            $res = $flowertype->get($id);
          //print_r($res);exit;
            $result = $flower->where('typeid',$id)->select();
            $count = $flower->where('typeid',$id)->count();
          //echo $count;exit;
            if(count($res)>0){
                $data['code']='1001';//1001表示有数据
                $data['status']='1';//1表示接口调用成功
                $data['data']=array('typeinfo'=>$res,'flowerinfo'=>$result);
            }else{
                $data['code']='1000';//1000表示没有查到数据
                $data['status']='1';//1表示接口调用成功
                $data['data']=array();
            }
        }else{
            $data['code']='1000';//1000表示没有数据
            $data['status']='0';//0表示接口调用失败
            $data['data']=array('缺少参数');
        }
        json($data)->send();
    }
    /**
     * 根据花卉的种类获取花卉
     */
    public function getFlowersdetails(){
        $id = input('id');
        if(isset($id)){
            $flower = new model\FlowerFlowerinfoModel();
            $address = new model\FlowerAddressModel();
            $res = $address->select();
            $result = $flower->where('id',$id)->select();
            $count = $flower->where('id',$id)->count();
            if($count>0){
                $data['code']='1001';//1001表示有数据
                $data['status']='1';//1表示接口调用成功
                $data['data']=$result;
                $data['address']=$res;
            }else{
                $data['code']='1000';//1000表示没有查到数据
                $data['status']='1';//1表示接口调用成功
                $data['data']=array();
                $data['address']=$res;
            }
        }else{
            $data['code']='1000';//1000表示没有数据
            $data['status']='0';//0表示接口调用失败
            $data['data']=array('缺少参数');
        }
        json($data)->send();
    }
    /**
     * 获取帮助中心的信息
     */
    public function getCenterInfo(){
        $center = new model\FlowerConfigModel();
        $result = $center->get(1);
        if(!empty($result)){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示没有查到数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=array();
        }
        json($data)->send();
    }

    /**
     * 获取提货点
     */
    public function getAddress(){
        $address = new model\FlowerAddressModel();
        $result = $address->all();
        if(!empty($result)){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示没有查到数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=array();
        }
        json($data)->send();
    }

    /**
     * 检测用户是否绑定手机号
     */
    public function checkphone(){
        $openid = input('openid');
        if(empty($openid)){
            $data['code']='1000';//1000表示无数据
            $data['status']='0';//0表示接口调用失败
            $data['data']='缺少参数';
        }else{
            $user = new model\FlowerUserinfoModel();
            $result = $user->where('phone !=""')->where(array('openid'=>$openid))->select();
            $result = json_encode($result);
            $result = json_decode($result);
            if(count($result)>0){
                $data['code']='1001';//1001表示有数据
                $data['status']='1';//1表示接口调用成功
                $data['data']=$result;
            }else{
                $data['code']='1000';//1000表示无数据
                $data['status']='1';//1表示接口调用成功
                $data['data']='没有对应数据';
            }
        }
        json($data)->send();
    }

    /**
     * 绑定用户手机号
     */
    public function addphone(){
        $user = new model\FlowerUserinfoModel();
        $openid = input('openid');
        $phone = input('phone');

        if(empty($phone)){
            $data['code']='1000';//1000表示无数据
            $data['status']='0';//0表示接口调用失败
            $data['data']='缺少参数';
        }else{
            $result = $user->where('openid',$openid)->update(array('phone'=>$phone,'dottime'=>time()));
            if($result>0){
                $data['code']='1001';//1001表示有数据
                $data['status']='1';//1表示接口调用成功
                $data['data']='更新成功';
            }else{
                $data['code']='1000';//1000表示无数据
                $data['status']='1';//1表示接口调用成功
                $data['data']='更新失败';
            }
        }
        json($data)->send();
    }
    /**
     * 根据用户的openid获取用户的订单，1:下单未支付，2：已经支付未完成，3：完成未评价，4：已经评价，5：订单完成
     */
    public function getOrders(){

        $user = new model\FlowerOrdersModel();
        // $openid = input('openid');
        $openid =Session::get('openid');
        // $openid = Session::get('openid');
        $status = input('status');//获取用户的不同类型的订单
        if(empty($status)){
            $result = $user->where(array('userid'=>$openid))->select();
        }else{
            $result = $user->where(array('userid'=>$openid,'status'=>$status))->select();
        }

        $result = json_encode($result);
        $result = json_decode($result);
        if(count($result)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='更新失败';
        }
        json($data)->send();
    }
    public function getOrdersdetails(){
        $user = new model\FlowerOrdersModel();
        $id = input('id');//获取订单id获取订单详情
        $result = $user->get($id);
        $result = json_encode($result);
        $result = json_decode($result);
        $orderid = $result->weixinorders;
        $result2 = Db::name('flower_comment')->where('orderid',$orderid)->find();
        if(count($result2)>0){
            $data['pinglun']=$result2['content'];
        }else{
            $data['pinglun']='';
        }
        if(count($result)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;

        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='更新失败';
        }
        json($data)->send();
    }
    /**
     *获取用户的信息
     */
    public function getuserinfo(){
        $openid =Session::get('openid');//获取openid

        $user = new model\FlowerUserinfoModel();
        $config = new model\FlowerConfigModel();
        $result2 = Db::name('flower_integrals')->select();
        $result =$config->get(1);
        $res = $user->get(array('openid'=>$openid));
        if(count($user)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$res;
            $data['usercenter']=$result;
            $data['integrals']=$result2;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='更新失败';
        }
        json($data)->send();
    }

    /**
     * 获取图文内容
     * {"code":"1001","status":"1","data":[{"username":"网站，防守对方是否","content":"好看"},{"username":"遇见美好的自己","content":"好看好看"}],"chatdetails":{"1":[{"username":"网站，防守对方是否","content":"好看"},{"username":"遇见美好的自己","content":"好看好看"}]}}
     */
    public function getChats(){
        // Session::set('userid','oCG5CxMLFva7ei2s_a0G2Nrb');
        //$openid =Session::get('openid');

        $page = input('page');
      
        $page =($page==1?0:($page-1)*5);
        $chats = new model\FlowerChatsModel();
        $result = $chats->alias('c')->join('flower_flower_userinfo u','c.userid = u.id')->field('u.username,u.img as headimgs,c.titile,c.content,c.img,c.dianzhan,c.id')->where('c.status',0)->order('c.id','desc')->limit($page,5)->select();
        //echo $chats->alias('c')->getLastSql();exit;
        $chatdetails = [];
        foreach($result as $k =>$v){
            $result2 = Db::name('flower_chatsdetails')->alias('c')->join('flower_flower_userinfo u','c.userid = u.id')->field('u.username,c.content')->where('c.chatsid',$v['id'])->select();
            $chatdetails[$v['id']] = $result2;
        }
        $result =json_encode($result);
        $result =json_decode($result);
        $res= $result;
        // var_dump($result);exit;
        /**
         *   $imgs = stripslashes($res['images']);//addslashes(json_encode($data['imgs']));
        $imgs = json_decode($imgs);
         */
        foreach($result as $ks =>$vs){
            if(!empty($vs->img)&&count($vs->img)>0){
                // var_dump($vs->img);exit;
                //$result[$ks]->img = json_decode(stripslashes($vs->img));

                $img = $vs->img;
                $arr =[];
                foreach($img as $k =>$v){
                    $arr[$k] = 'http://'.$_SERVER['HTTP_HOST'].'/'.($v);
                }
                $res[$ks]->img = $arr;
            }
        }

        if(count($result)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$res;
            $data['chatdetails']=$chatdetails;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='查询失败';
        }
        json($data)->send();
    }

    /**
     * 点赞功能，参数有id:帖子的id
     */
    public function dianzhan(){

        $id = input('id');//帖子的id
        $openid =Session::get('openid');
        //$openid = 'oCG5CxMLFva7ei2s_a0G2Nrb';
        /* $userid = Session::get('useridss');
         echo $userid;exit;
          //echo $userid;exit;
         if($userid==$openid){*/
        // }
        /*   $diantime =  Session::get('diantime');
            $timediff = time()-$diantime;
            $days = intval($timediff/86400);

            if($days <1){
                $data['code']='1002';//1002表示一天中不能重复点赞
                $data['status']='1';//1表示接口调用成功
                $data['data']='';
                json($data)->send();exit;
            }*/

        $chats = new model\FlowerChatsModel();
        $result = $chats->get($id);
        $dianzhan = $result['dianzhan'];
        $dianzhan++;
        $datas['dianzhan'] = $dianzhan;
        $res = $chats->where('id',$id)->update($datas);
        if($res>0){
            Session::set('userid',$openid);
            /*Session::set('diantime',time());
            Session::delete('useridss');
            Session::set('useridss',$openid);*/
            //$_SESSION['userid'] = $openid;
            //$userid = Session::get('userid');

            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$datas;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='设置失败';
        }
        json($data)->send();
    }

    /**
     * 评论
     */
    public function pinglun(){
        $id = input('id');//帖子的id
        $content = empty(input('content'))?'':input('content');//帖子的id
        $userid = Session::get('openid');
        $res = Db::name('flower_userinfo')->where('openid',$userid)->find();
        $datas['chatsid'] = $id;
        $datas['userid'] = $res['id'];
        $datas['content'] = $content;
        $datas['dottime'] =time();
        $res = Db::name('flower_chatsdetails')->insert($datas);
        if($res>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功

            $result2 = Db::name('flower_chatsdetails')->alias('c')->join('flower_flower_userinfo u','c.userid = u.id')->field('u.username,c.content')->where('c.chatsid',$id)->select();

            $data['data']=$result2;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='设置失败';
        }
        json($data)->send();

    }

    /**
     * 发表图文
     */
    public function fabiao(){
        $content =input('content');
        //获取上传的图片
        $files = request()->file('pics');

        $imgs = [];
        foreach($files as $file){
            // 移动到框架应用根目录/public/uploads/  目录下
            $info = $file->validate(['size'=>1048576,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'uploads/flower/');

            if($info){
                $url = '/uploads/flower/'.$info->getSaveName();
                array_push($imgs,$url);
            }else{
                // 上传失败获取错误信息
                echo $file->getError();exit;
            }
        }

        $imgs = json_encode($imgs);//addslashes(json_encode($data['imgs']));
        $imgs = addslashes($imgs);
        $userid = Session::get('openid');
        $res = Db::name('flower_userinfo')->where('openid',$userid)->find();
        $id = $res['id'];//用户的id
        $datas['userid'] = $id;
        $datas['img'] = $imgs;
        $datas['content'] = $content;
        $datas['dottime'] = time();
        $datas['dianzhan'] = 0;
        $result = Db::name('flower_chats')->insert($datas);
        if($result>0){
            $this->redirect(url.'/luntan.html');
        }

    }

    /**
     * 获取用户说的内容
     */
    public function getguestsshuo(){
        $result = Db::name('flower_guestpinglun')->where('deltime',0)->select();
        $result =json_encode($result);
        $result =json_decode($result);
        foreach($result as $k => $v){
            if(!empty($v->img)) {
                //$result[$k]->img = 'http://'.$_SERVER['HTTP_HOST'].'/public/upload/'.($v->img);
                $result[$k]->img = 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.($v->img);
            }
            if(!empty($v->headimg)) {
                $result[$k]->headimg =  'http://'.$_SERVER['HTTP_HOST'].'/upload/'.($v->headimg);
            }
        }
        if(count($result)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='设置失败';
        }
        json($data)->send();
    }

    /**
     * 获取网站的咯logo
     */
    public function getlogos(){
        $result = Db::name('flower_logos')->where('id',1)->find();
        $result['logo'] = 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$result['logo'];
        $result['guestshuo'] = 'http://'.$_SERVER['HTTP_HOST'].'/upload/'.$result['guestshuo'];
        $data['code']='1001';//1001表示有数据
        $data['status']='1';//1表示接口调用成功
        $data['data']=$result;
        json($data)->send();
    }
    /**对订单评论*/
    public function comment(){
        $orderid = input('orderid');
        $content = input('content');
        $openid =Session::get('openid');
        $data['content'] = $content;
        $data['orderid'] = $orderid;
        $data['userid'] = $openid;
        $data['dottime'] = time();
        $result = Db::name('flower_comment')->insert($data);
        // echo Db::name('flower_comment')->getLastSql();exit;
        $result1 = Db::name('flower_orders')->where('weixinorders',$orderid)->update(array('status'=>4));
        if($result>0&&$result1>0){
            $datas['code']='1001';//1001表示有数据
            $datas['status']='1';//1表示接口调用成功
            $datas['data']='评价陈功';
        }else{
            $datas['code']='1000';//1000表示无数据
            $datas['status']='1';//1表示接口调用成功
            $datas['data']='失败';
        }
        json($datas)->send();
    }
    /**
     * 订单完成
     */
    public function orderComplete(){
        $user = new model\FlowerOrdersModel();
        $orderid = input('orderid');
        $result = $user->where('weixinorders',$orderid)->update(array('status'=>3,'etime'=>time()));

        if($result>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;

        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='更新失败';
        }
        json($data)->send();
    }
    /**
     * 打折
     */
    public function discount(){
        $user = new model\FlowerUserinfoModel();
        $config =  Db::name('flower_integrals')->select();
        $openid =Session::get('openid');
        $result = $user->where('openid',$openid)->find();

        if(count($result)>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['userinfo']=$result;
            $data['config']=$config;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='失败';
        }
        json($data)->send();
    }
    /**
     * 提交需要修改的订单的信息
     */
    /*public function  orderchange(){
        $orderid = input('orderid');
        $time = input('time');
        $address = input('address');
        $phone = input('phone');
        $datas['orderid'] = $orderid;
        $datas['time'] = $time;
        $datas['address'] = $address;
        $datas['phone'] = $phone;
        $datas['dottime'] = time();
        $result = Db::name('flower_orderchange')->insert($datas);
        if($result>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='失败';
        }
        json($data)->send();
    }*/
  /**
     * 提交需要修改的订单的信息
     */
    public function  orderchange(){
        $orderid = input('orderid');
        $time = input('time');
        $result = Db::name('flower_orders')->where('order',$orderid)->find();
        $ti=$result['endtime'];
        if($time!=''&&$result['endtime']!=''){
            $tis = date('Y-m-d H:i:s',$ti+3*7*24*60*60);

            $tis = strtotime($tis);
           // echo date('Y-m-d H:i:s',$result['endtime']).'/'.$tis;exit;
        }else{
            $tis= $ti;
        }
        $address = input('address');
        $phone = input('phone');
        $datas['orderid'] = $orderid;
        $datas['time'] = $tis;
        $datas['address'] = $address;
        $datas['phone'] = $phone;
        $datas['dottime'] = time();

       // echo $tis.'/'.$result['endtime'].'/'.$time;exit;
        $datass['endtime'] = $tis;
        $datass['address'] = $address;
        $result2 =  Db::name('flower_orderchange')->insert($datas);
        $result = Db::name('flower_orders')->where('order',$orderid)->update($datass);
        if($result>0&&$result2>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='失败';
        }
        json($data)->send();
    }
      /**
     * 取消订单
     */
    public function orderCancel(){
        $orderid = input('orderid');
        $datas['order'] = $orderid;
        $datas['remarks'] = '该订单已经取消';
        $datas['endtime'] = time();
        $datas['status'] = 5;
        $result = Db::name('flower_orders')->where('order',$orderid)->update($datas);
        if($result>0){
            $data['code']='1001';//1001表示有数据
            $data['status']='1';//1表示接口调用成功
            $data['data']=$result;
        }else{
            $data['code']='1000';//1000表示无数据
            $data['status']='1';//1表示接口调用成功
            $data['data']='失败';
        }
        json($data)->send();
    }
}